﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vngoodlife.Models;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace vngoodlife.Util
{
    public class UrlExtensions
    {
        public static Dictionary<UrlModel, UrlModel> GetDictionary()
        {
            Dictionary<UrlModel, UrlModel> urls = new Dictionary<UrlModel, UrlModel>();
            //urls.Add(new UrlModel { Controller = "Biography", Action = "Index" }, new UrlModel { Controller = "About", Action = "Index" });
            urls.Add(new UrlModel { Controller = "Article", Action = "Details" }, new UrlModel { Controller = "Article", Action = "Details" });
            return urls;
        }
    }

    public static class HtmlExtensions
    {
        public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, String linkText, String actionName, String controllerName)
        {
            var tag = new TagBuilder("li");
            //var tag = new TagBuilder("ul");


            if (htmlHelper.ViewContext.RequestContext.IsCurrentRoute(null, controllerName, actionName) ||
                htmlHelper.ViewContext.RequestContext.IsParentRoute(controllerName, actionName))
            {
                tag.AddCssClass("active");
                //tag.AddCssClass("current");
            }
            else
            {
                tag.AddCssClass("inactive");
                //tag.AddCssClass("select");
            }

            tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToString();

            return MvcHtmlString.Create(tag.ToString());
        }
    }

    public static class RequestExtensions
    {
        public static bool IsCurrentRoute(this RequestContext context, String areaName)
        {
            return context.IsCurrentRoute(areaName, null, null);
        }

        public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName)
        {
            return context.IsCurrentRoute(areaName, controllerName, null);
        }

        public static bool IsCurrentRoute(this RequestContext context, String areaName, String controllerName, params String[] actionNames)
        {
            var routeData = context.RouteData;
            var routeArea = routeData.DataTokens["area"] as String;
            var current = false;

            if (((String.IsNullOrEmpty(routeArea) && String.IsNullOrEmpty(areaName)) || (routeArea == areaName)) &&
                 ((String.IsNullOrEmpty(controllerName)) || (routeData.GetRequiredString("controller") == controllerName)) &&
                 ((actionNames == null) || actionNames.Contains(routeData.GetRequiredString("action"))))
            {
                current = true;
            }

            return current;
        }

        public static bool IsParentRoute(this RequestContext context, String controller, String action)
        {
            var routeData = context.RouteData;
            UrlModel returnUrl = null;
            UrlModel requestUrl = new UrlModel { Action = routeData.GetRequiredString("action"), Controller = routeData.GetRequiredString("controller") };
            UrlModel linkUrl = new UrlModel { Action = action, Controller = controller };

            var urls = UrlExtensions.GetDictionary();
            urls.TryGetValue(requestUrl, out returnUrl);

            if (returnUrl != null && returnUrl.Equals(linkUrl))
                return true;
            else
                return false; ;
        }
    }
}